package scalapackage

import scala.collection.mutable._

/**
  * Created by Germmy on 2018/2/25.
  */
class MyFirstScala {

}

object ScalaObject{
  def main (args: Array[String] ) {
  val s="abc"
//    println(s)


    var myvar1="cde";
//    println(myvar1)


    val a :Int =456;
//    println(a)


    val s1 : String ="china"
//    println(s1)

    var x=1;
    var s2= if(x>2) 1 else 2
//    println(s2)



    val z=if(x>2) 1 else "error"
//    println(z)


    val z2= if (x>2) 1
//    println(z2)

    val z3=if(x>2) 1 else ()
//    println(z3)

    val z4=if(x>2) 1 else  if (x>3) 2 else 0
//    println(z4)



//    for(i<- 1 to 10) println(i)


//    for(i<-1 to 20){
//      println(i)
//    }

//    val arr=Array("china","japan","america")
//    for(i<-arr){
//      println(i)
//    }


//    for(i<-1 to 3){
//      for(j<- 1 to 4){
//        if(i!=j) println(i + "====" + j)
//      }
//    }


    val r=for(i <- 1 to 10 ) yield  i
//    println(r)

//    def m1(x:Int,y:Int) :Int=x+y


//    val r2=m1(3,4)
//    println(r2)


    val r3=(x:Int,y:Int)=>x+y

    val r4=r3(3,4)
//    println(r4)

    //def方法,val r=xxx是函数

    def m5(f:(Int,Int)=>Int)=f(4,5)

    val f1=(x:Int,y:Int)=>x.*(y)

    //函数经常被当作参数传到方法中
    val r6=m5(f1)
//    println(r6)


    def m1(x:Int , y:Int)=x+y

    val f2=m1 _

//    val r7=m5(f2)
    val r7=m5(m1)
//    println(r7)



    var myArr1=new Array[Int](8)
//    println(myArr1)
//    println(myArr1.toBuffer)

//    for(i<-myArr1) println(i)

//    println(myArr1(1))


    var myArr2=new ArrayBuffer[Int]();
    myArr2+=1
    myArr2+=(2,3,5)
    myArr2++=myArr1
    myArr2.insert(0,9,1)
    myArr2.remove(4,2)
//    println(myArr2.toBuffer)

//    for(i<-0 until myArr2.length) println(myArr2(i))
//    for(i<-(0 until myArr2.length).reverse) println(myArr2(i))


    var myArry3=for(i<-myArr2) yield i*10;
//    println(myArry3)

//    println(myArr2.sum)
    var myNewArr=myArr2.sorted//这个排序死活不生效，日
//    println(myNewArr)


    /***
      * 以下是映射
      */


    val map1=scala.collection.mutable.Map("scala"->1,"c#"->2,"java"->3)

    val r1=map1("scala")
    map1("scala")=6
    val r11=map1("scala")
//    println(r11)


    val map2=Map(("scala",1),("erlang",2),("go",3))
    val r2=map2("erlang")
    val r31=map2.getOrElse("python",-1)


//    println(r2)
//    println(r31)

    map2("gogogo")=4



    val list1=List(1,2,3)
    val list2=0 :: list1;

    val list3= list1.::(0)
//    println(list2)
//    println(list3)


    val list4=0 +: list1;
//    println(list4)

    val list5=list1.+:(0)
//    println(list5)

    val list6=list1.:+(4)
//    println(list6)


    /**
      * 2个list合并
      */
    val list7=List(5,6,7)

    val list8=list1 ++ list7
//    println(list8)


    val list9=list1 ++: list7
//    println(list9)


    val list10=list1 ::: list7
//    println(list10)



    val varList11=ListBuffer(1,2,3)
    println(varList11 += 4)
    varList11.append(5)
    println(varList11)

    val varList12=ListBuffer(14,15,16)
    varList11 ++= varList12
    println(varList11)


    val varList13=varList11 ++ varList12
    println(varList11)


    val varList14=varList11 :+ 4
    println(varList11)
    println(varList14)


    val varList15=varList11.+:(5)
    println(varList11)
    println(varList15)




}
}
